<?php
	include('config.php');

	if(isset($_COOKIE['session_id']) && isset($_COOKIE['user_id'])){
		$hash = addslashes($_COOKIE['session_id']);
		$uid = addslashes($_COOKIE['user_id']);
		
		$query = 'SELECT U.user_id FROM USERS U WHERE user_id = '. $uid . ' AND password = \'' . $hash . '\'';		
		$results = mysql_query($query);
	
		if(mysql_num_rows($results) >= 1){		
			$query = 'SELECT u.username, u.email, posts FROM USERS u JOIN (SELECT COUNT(*) "posts" FROM COMMENTS WHERE user_id = ' . $id . ') c WHERE user_id = ' . $id;
			$results = mysql_query($query);
			
			$row = mysql_fetch_array($results);
			
			if($uid == $id){
				echo '<table border="4" id="generic">';
				echo '<tr> <td><b>Username</b></td> <td><b>Email</b></td> <td><b>Post Count</b></td> </tr>';
				echo '<tr><td>' . $row['username'] . '</td><td>' . $row['email'] . ' <form name="email_change" method="GET" action="javascript:  void(0);">
						<input name="email" type="hidden" value="" />
						<input type="submit" value="change" onclick="changeEmail()" />
						</form></td><td>' . $row['posts'] . '</td></tr>';
				echo '</table><br /><br /><br />';
				
			}else{
				echo '<table border="4" id="generic">';
				echo '<tr> <td><b>Username</b></td> <td><b>Email</b></td> <td><b>Post Count</b></td> </tr>';
				echo '<tr><td>' . $row['username'] . '</td><td>' . $row['email'] . '</td><td>' . $row['posts'] . '</td></tr>';
				echo '</table><br /><br /><br />';
			}
			
			$query = 'SELECT t.topic, t.thread_id, t.last_post FROM THREADS t JOIN USERS u WHERE u.username = t.username AND u.user_id = ' . $id;
			$results = mysql_query($query);
			
			$count = 0;
			while($row = mysql_fetch_array($results)){
				if($count == 0){
					echo '<br /><br /><h3>Threads Started By This User</h3>';
					echo '<table border="4" id="generic">';
					echo '<tr><td><b>Thread Topic</b></td> <td><b>Last Post</b></td></tr>';
				}
				
				echo '<tr><td><a href="index.php?page=threads&id=' . $row['thread_id'] . '">' . $row['topic'] . "</a></td><td>" . 
				$row['last_post'] . 	'</td></tr>';
				
				$count ++;
			}
			
			if($count == 0){
				echo '<h3>This user hasn\'t started any threads!</h3>';
			}else{
				echo '</table>';
			}
			
			echo '<br /><br /><br />';
			
			$query = 'SELECT T.thread_id, T.topic, C.comment_text FROM THREADS T JOIN COMMENTS C WHERE T.thread_id = C.thread_id AND C.user_id = '. $id;
			
			$results = mysql_query($query);
			
			$count = 0;
			while($row = mysql_fetch_array($results)) {
				if($count == 0){
					echo '<h3>Comment History</h3>';
					echo '<table border="4" id="generic">';
					echo '<tr><td><b>Thread Topic</b></td> <td><b>Comment</b></td></tr>';
				}
				
				$text = stripslashes($row['comment_text']);
				if(stristr($text, '[java]') == false && stristr($text, '[xml]') == false){
					$text = nl2br($text);
				}else{					
					if(stristr($text, '[java]') != false){
						if(stristr($text, '[/java]') == false){
							$text = $text . '[/java]';
						}
						
						$before = nl2br(substr($text, 0, strpos($text, '[java]')));
						$after = nl2br(substr($text, strpos($text, '[/java]') + 7, strlen($text) - strpos($text, '[/java]') + 7));;
						$text = substr($text, strpos($text, '[java]'), strpos($text, '[/java]') - strpos($text, '[java]') + 7);
						
						$text = str_replace('[java]', '<pre class="brush:java; class-name: \'code2\'">', $text);
						$text = str_replace('[/java]', '</pre>', $text);

						$text = $before . $text . $after;
					}
					
					if(stristr($text, '[xml]') != false){
						if(stristr($text, '[/xml]') == false){
							$text = $text . '[/xml]';
						}
						
						$before = nl2br(substr($text, 0, strpos($text, '[xml]')));
						$after = nl2br(substr($text, strpos($text, '[/xml]') + 7, strlen($text) - strpos($text, '[/xml]') + 7));;
						$text = substr($text, strpos($text, '[xml]'), strpos($text, '[/xml]') - strpos($text, '[xml]') + 7);
						
						$text = str_replace('[xml]', '<pre class="brush:xml; class-name: \'code2\'">', $text);
						$text = str_replace('[/xml]', '</pre>', $text);
						
						$text = $before . $text . $after;
					}
				}
				
				echo '<tr><td><a href="index.php?page=threads&id=' . $row['thread_id'] . '">' . $row['topic'] . "</a></td><td>" . $text . '</td></tr>';
				
				$count ++;
			}	
			
			if($count == 0){
				echo '<h3>This user hasn\'t commented yet!</h3>';
			}else{
				echo '</table>';
			}
		}else{
			echo '<h3>You must be logged in to view profiles!</h3>';
		}
	}else{
		echo '<h3>You must be logged in to view profiles!</h3>';
	}
	
	mysql_close($mysql_handle);
?>